package com.baidu.health.service.impl;

import com.baidu.health.mapper.TbAilmentFatorMapper;
import com.baidu.health.mapper.TbFactorMapper;
import com.baidu.health.pojo.ResultResponse;
import com.baidu.health.pojo.TbAilmentFator;
import com.baidu.health.pojo.TbFactor;
import com.baidu.health.service.TbFactorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbFactorServiceImpl extends ServiceImpl<TbFactorMapper, TbFactor>
    implements TbFactorService{
    @Autowired
    private TbFactorMapper tbFactorMapper;
    @Autowired
    private TbAilmentFatorMapper tbAilmentFatorMapper;
    @Override
    public ResultResponse factorSelect(TbFactor tbFactor) {
        QueryWrapper queryWrapper=new QueryWrapper();

        if(!StringUtils.isEmpty(tbFactor.getFactorName())){
            queryWrapper.eq("factor_id",queryWrapper);
        }
        List<TbFactor> list = tbFactorMapper.selectList(queryWrapper);
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse factorInsert(TbFactor tbFactor) {
        tbFactor.setCreateTime(new Date());
        tbFactorMapper.insert(tbFactor);
        return ResultResponse.SUCCESS("添加成功");
    }

    @Override
    public ResultResponse factorUpdate(TbFactor tbFactor) {
        tbFactorMapper.updateById(tbFactor);
        return ResultResponse.SUCCESS("修改成功");
    }

    @Override
    public ResultResponse factorDeleted(Integer factorId) {
        tbFactorMapper.deleteById(factorId);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("factor_id",factorId);
        List<TbAilmentFator> list = tbAilmentFatorMapper.selectList(queryWrapper);
        if(list!=null){
            for (int i = 0; i < list.size(); i++) {
                Integer riskId = list.get(i).getRiskId();
                tbAilmentFatorMapper.deleteById(riskId);
            }
        }
        return ResultResponse.SUCCESS("删除成功");
    }
}




